package org.hdz.pcmdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

/**
 * @author hdz
 */
public class PlayActivity extends AppCompatActivity implements View.OnClickListener {

    private String mAssetsPcmFile = "16k_16_1.pcm";
    private boolean mPlaying = false;
    private MyAudioPlay mAudioPlay;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case Constant.PLAY_STATE_COMPLETE:

                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        initView();
    }
    private void initView() {
        Button btnStartPlayPcm = (Button)findViewById(R.id.btnStartPlayPcm);
        btnStartPlayPcm.setOnClickListener(this);
        Button btnStopPlayPcm = (Button)findViewById(R.id.btnStopPlayPcm);
        btnStopPlayPcm.setOnClickListener(this);

        RadioButton rBtn8kPcm = (RadioButton)findViewById(R.id.rBtn8kPcm);
        rBtn8kPcm.setOnClickListener(this);
        RadioButton rBtn16kPcm = (RadioButton)findViewById(R.id.rBtn16kPcm);
        rBtn16kPcm.setOnClickListener(this);
        RadioButton rBtn44kPcm = (RadioButton)findViewById(R.id.rBtn44kPcm);
        rBtn44kPcm.setOnClickListener(this);

        rBtn16kPcm.setChecked(true);
        AudioParams pcmParamOut = AudioParams.GetPCMParamByDecodeType(AudioParams.AUDIO_16K_PCM, false);
        mAudioPlay = new MyAudioPlay(this, mHandler, pcmParamOut);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnStartPlayPcm:
                mAudioPlay.play(1, mAssetsPcmFile);
                break;
            case R.id.btnStopPlayPcm:
                mAudioPlay.stop();
                break;
            case R.id.rBtn8kPcm:
                Toast.makeText(this, "--", Toast.LENGTH_SHORT).show();
                mAssetsPcmFile = "8k_16_1.pcm";
                if (mAudioPlay != null) {
                    mAudioPlay.release();
                }
                mAudioPlay = new MyAudioPlay(this, mHandler, AudioParams.GetPCMParamByDecodeType(AudioParams.AUDIO_LOW_PCM, false));
                break;
            case R.id.rBtn16kPcm:
                mAssetsPcmFile = "16k_16_1.pcm";
                if (mAudioPlay != null) {
                    mAudioPlay.release();
                }
                mAudioPlay = new MyAudioPlay(this, mHandler, AudioParams.GetPCMParamByDecodeType(AudioParams.AUDIO_16K_PCM, false));
                break;
            case R.id.rBtn44kPcm:
                mAssetsPcmFile = "44.1_16_1.pcm";
                if (mAudioPlay != null) {
                    mAudioPlay.release();
                }
                mAudioPlay = new MyAudioPlay(this, mHandler, AudioParams.GetPCMParamByDecodeType(AudioParams.AUDIO_HIGH_PCM, false));
                break;
            default:
                break;
        }
    }
}
